package cn.inbot.shopadmin.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import cn.inbot.shopadmin.R;
import cn.inbot.lib.util.StringUtil;

/**
 * Created by shisenkun on 2021/5/10.
 */
public final class SettingItemLayout extends ConstraintLayout {

    private boolean showDivider;
    private String name;
    private int logoResId;
    private View divider;
    private TextView tvName;
    private ImageView ivLogo;


    public SettingItemLayout(Context context) {
        this(context, null);
    }

    public SettingItemLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initSettingItemLayout(context, attrs);
    }

    private void initSettingItemLayout(Context context, AttributeSet attrs) {
        View.inflate(context, R.layout.layout_setting_item, this);
        divider = findViewById(R.id.divider);
        tvName = findViewById(R.id.tvName);
        ivLogo = findViewById(R.id.ivLogo);

        setBackgroundResource(R.drawable.bg_setting_item);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SettingItemLayout);
        this.showDivider = ta.getBoolean(R.styleable.SettingItemLayout_show_divider, false);
        this.name = ta.getString(R.styleable.SettingItemLayout_name);
        this.logoResId = ta.getResourceId(R.styleable.SettingItemLayout_logo, this.logoResId);
        ta.recycle();

        divider.setVisibility(showDivider ? View.VISIBLE : View.GONE);
        if (StringUtil.isNotEmpty(name)) {
            tvName.setText(name);
        }
        if (logoResId != -1) {
            ivLogo.setImageResource(logoResId);
        }
    }

}
